﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MaxStudio.Concrete.Web;
using MaxStudio.Concrete.DBUtility;
using MaxStudio.Concrete.Server;
using MaxStudio.Concrete.Server.Service;
using MaxStudio.Concrete.Server.Dao;
using System.Configuration; 

namespace MaxStudio.Concrete.Web.Pages
{
    public partial class RPTAdmixtureAllocate : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGridView();
            }
        }
        #region Private Method

        private void BindGridView()
        {
            RPTAdmixtureAllocateService RPTAdmixtureAllocateServiceObj = ServiceFactory.getRPTAdmixtureAllocateService();
            OperationResult<List<tb_RPT_AdmixtureAllocate>> result = RPTAdmixtureAllocateServiceObj.GetAllAdmixtureAllocate();

            if (result.operationSuccess)
            {
                gvAdmixtureAllocateList.DataSource = result.obj;
                gvAdmixtureAllocateList.DataBind();
            }
            else
            {
                //EXCEPTION
                //return null;
            }
        }

        private void BindEdieDialog(int id)
        {
            RPTAdmixtureAllocateService RPTAdmixtureAllocateServiceObj = ServiceFactory.getRPTAdmixtureAllocateService();
            OperationResult<tb_RPT_AdmixtureAllocate> resultEdit = RPTAdmixtureAllocateServiceObj.GetAdmixtureAllocateById(id);

            if (resultEdit.operationSuccess == true)
            {
                txtEditAllocateDate.Text = resultEdit.obj.AllocateDate;
                txtEditPowdeDosage.Text = resultEdit.obj.PowdeDosage;
                txtEditLiquidWeight.Text = resultEdit.obj.LiquidWeight;
                txtEditTankNumber.Text = resultEdit.obj.TankNumber;
                txtEditRemark.Text = resultEdit.obj.Remark; 

                lbLastModier.Text = "TEST";
                lbEidtId.Text = resultEdit.obj.Id.ToString();
                lbLastModifyDate.Text = resultEdit.obj.ModifyDate.ToShortDateString();  
            }
        }

        private void DeleteRecords(int id)
        {
            lbDeleteId.Text = id.ToString();
        }    

        #endregion

        #region Event
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            RPTAdmixtureAllocateService RPTAdmixtureAllocateServiceObj = ServiceFactory.getRPTAdmixtureAllocateService();
            tb_RPT_AdmixtureAllocate instance = new tb_RPT_AdmixtureAllocate();

            instance.AllocateDate = txtAllocateDate.Text;
            instance.PowdeDosage = txtPowdeDosage.Text;
            instance.LiquidWeight = txtLiquidWeight.Text;
            instance.TankNumber = txtTankNumber.Text;
            instance.Remark = txtRemark.Text;

            OperationResult<tb_RPT_AdmixtureAllocate> result = RPTAdmixtureAllocateServiceObj.CreateAdmixtureAllocate(instance);
            BindGridView();

        }
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            int editId = 0;
            if (lbEidtId.Text.Length > 0)
            {
                editId = Int32.Parse(lbEidtId.Text);
            }

            RPTAdmixtureAllocateService RPTAdmixtureAllocateServiceObj = ServiceFactory.getRPTAdmixtureAllocateService();
            tb_RPT_AdmixtureAllocate entity = new tb_RPT_AdmixtureAllocate();

            entity.Id = editId;
            entity.AllocateDate = txtEditAllocateDate.Text;
            entity.PowdeDosage = txtEditPowdeDosage.Text;
            entity.LiquidWeight = txtEditLiquidWeight.Text;
            entity.TankNumber = txtEditTankNumber.Text;
            entity.Remark = txtEditRemark.Text; 

            entity.ModifyDate = DateTime.Now;
            entity.ModifyBy = 1;
            entity.CreateDate = DateTime.Now;

            OperationResult<tb_RPT_AdmixtureAllocate> resultEdit = RPTAdmixtureAllocateServiceObj.ModifyAdmixtureAllocate(entity);

            BindGridView();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int deleteId = Int32.Parse(lbDeleteId.Text);
            RPTAdmixtureAllocateService RPTAdmixtureAllocateServiceObj = ServiceFactory.getRPTAdmixtureAllocateService();
            tb_RPT_AdmixtureAllocate deleteEntity = new tb_RPT_AdmixtureAllocate();
            deleteEntity.Id = deleteId;
            RPTAdmixtureAllocateServiceObj.DeleteAdmixtureAllocate(deleteEntity);

            BindGridView();
        }

        protected void gvAdmixtureAllocateList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                string strSelectId = e.CommandArgument.ToString();
                BindEdieDialog(Int32.Parse(strSelectId));
            }

            if (e.CommandName == "del")
            {
                string strSelectId = e.CommandArgument.ToString();
                DeleteRecords(Int32.Parse(strSelectId));
            }
        }

        protected void gvAdmixtureAllocateList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvAdmixtureAllocateList.PageIndex = e.NewPageIndex;
            BindGridView();
        }
        #endregion
    }
}
